<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/5/31
 * Time: 15:31
 */
namespace app\api\controller;
use app\api\model\UserModel;
use think\Controller;
use think\Db;
class Login extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->user = new UserModel();
      	header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }
    /**
     * 注册
    */
    public function registerd()
    {
        $res = $this->user->registerd();
      	echo json_encode($res);
    }
    /**
     * 登陆
    */
    public function login()
    {
        $res = $this->user->login();
        return json_encode($res);
    }
  		//短信验证码
	public function sends()
    {
        Db::startTrans();
      	$Mobile = input('phone');//$_POST['Mobile']; //手机号
      	if(!$Mobile){
          echo json_encode(['code'=>"0",'message'=>"手机号不能为空！"],true);die;
        }
        $CorpID = "QTTX009177";//$_POST['CorpID']; //用户名
		$Pwd = "lv112358";//$_POST['Pwd']; //密码
		$code =  rand(100000,999999);//生成验证码
		$Content = "您好，您的验证码是".$code."。如非本人操作请勿泄露给他人"; //内容
		$ContentS = rawurlencode(mb_convert_encoding($Content, "gb2312", "utf-8"));//短信内容做GB2312转码处理
		$url = "https://sdk2.028lk.com/sdk2/LinkWS.asmx/BatchSend2?CorpID=".$CorpID."&Pwd=".$Pwd."&Mobile=".$Mobile."&Content=".$ContentS."&Cell=&SendTime=";
		$result=file_get_contents($url);
		$re=simplexml_load_string($result);
        $res=Db::name('sm')->insert(['phone'=>$Mobile,'code'=>$code,'createtime'=>time(),'ip'=>$_SERVER['REMOTE_ADDR'],'invalidtime'=>strtotime(date('Y-m-d H:i:s', strtotime('+3minute')))]);
		if($re&&$res){
          Db::commit();
          echo  json_encode(['code'=>"1",'message'=>"发送成功！"]);
        }else{
          Db::rollback();
          echo  json_encode(['code'=>"0",'message'=>"发送失败！"]);
        }
    }
}